package it.thegamers.maze;

import it.thegamers.maze.controls.MyCollisionLose;
import it.thegamers.maze.controls.MyCollisionWin;
import it.thegamers.maze.entities.Constants;

import java.awt.Color;
import java.awt.Graphics2D;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.ColorBackground;

/**
 * @author agente mm8
 */
public class Maze extends Game
{
    private ColorBackground gameBackground;
    
    private Sprite playerSprite, road, finish; // road=percorso; finish=arrivo
    
    private SpriteGroup PLAYER, ROAD, FINISH;
    
    private CollisionManager collisionWin, collisionLose;
    
    @Override
    public void initResources() {
        gameBackground = new ColorBackground(Color.black);
        
        playerSprite = new Sprite(getImage(Constants.IMG_PATH + "/player.png"));
        road = new Sprite(getImage(Constants.IMG_PATH + "/road2.png"));
        finish = new Sprite(getImage(Constants.IMG_PATH + "/finish.png"));
        
        PLAYER = new SpriteGroup("Player Group");
        PLAYER.setBackground(gameBackground);
        PLAYER.add(playerSprite);
        
        ROAD = new SpriteGroup("Road Group");
        ROAD.setBackground(gameBackground);
        ROAD.add(road);
        
        FINISH = new SpriteGroup("Finish Group");
        FINISH.setBackground(gameBackground);
        FINISH.add(finish);
        
        collisionWin = new MyCollisionWin();
        collisionWin.setCollisionGroup(PLAYER, FINISH);
        collisionLose = new MyCollisionLose();
        collisionLose.setCollisionGroup(PLAYER, ROAD);
    }
    
    @Override
    public void render(Graphics2D g) {
        gameBackground.render(g);
        
        PLAYER.render(g);
        ROAD.render(g);
        FINISH.render(g);
        
        if (!playerSprite.isActive()) {
        	g.setColor(Color.white);
            g.drawString("Complimenti, hai vinto!", 640 / 2, 480 / 2);
        }
        else if (!road.isActive()) {
        	g.setColor(Color.white);
            g.drawString("Mi dispiace, hai perso!", 640 / 2, 480 / 2);
        }
    }
    
    @Override
    public void update(long elapsedTime) {
        gameBackground.update(elapsedTime);
        
        PLAYER.update(elapsedTime);
        ROAD.update(elapsedTime);
        FINISH.update(elapsedTime);
        
        playerSprite.setLocation(this.getMouseX(), this.getMouseY());
        
        collisionWin.checkCollision();
        collisionLose.checkCollision();
    }
    
    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new Maze(), Constants.GAME_RESOLUTION, false);
        game.start();
    }
}